package com.cci.kangdao.utilTool;

import com.cci.kangdao.common.ApplicationContextUtils;
import com.cci.kangdao.common.RedisKeys;
import com.cci.kangdao.redis.RedisClientUtils;

public class SerialNumberUtil {

    /**
     * 获取递增后的值
     * @param prefix
     * @return
     */
    public static String getSerialNumber(String prefix) {
        String regTime = LocalTimeUtil.getRealCurrentTime();
        String strDate = regTime.substring(0, 10).replace("-", "");
        RedisClientUtils redisClientUtils = (RedisClientUtils) ApplicationContextUtils.getBean("redisClientUtils");
        Long newEquipment = redisClientUtils.incr(RedisKeys.workOrderNoKey(strDate), 24 * 60 * 60);
        while (newEquipment.equals(0L)) {
            newEquipment = redisClientUtils.incr(RedisKeys.workOrderNoKey(strDate), 24 * 60 * 60);
        }
        return String.format("%s%s%04d", prefix, strDate, newEquipment);
    }

    public static String getFieldServiceItemNumber(String prefix) {
        String regTime = LocalTimeUtil.getRealCurrentTime();
        String strDate = regTime.substring(0, 10).replace("-", "");
        RedisClientUtils redisClientUtils = (RedisClientUtils) ApplicationContextUtils.getBean("redisClientUtils");
        Long newEquipment = redisClientUtils.incr(RedisKeys.fieldServiceItemNoKey(strDate), 24 * 60 * 60);
        while (newEquipment.equals(0L)) {
            newEquipment = redisClientUtils.incr(RedisKeys.fieldServiceItemNoKey(strDate), 24 * 60 * 60);
        }
        return String.format("%s%s%04d", prefix, strDate, newEquipment);
    }

    public static String getFieldServiceOrderNumber(String prefix) {
        String regTime = LocalTimeUtil.getRealCurrentTime();
        String strDate = regTime.substring(0, 10).replace("-", "");
        RedisClientUtils redisClientUtils = (RedisClientUtils) ApplicationContextUtils.getBean("redisClientUtils");
        Long newEquipment = redisClientUtils.incr(RedisKeys.fieldServiceOrderNoKey(strDate), 24 * 60 * 60);
        while (newEquipment.equals(0L)) {
            newEquipment = redisClientUtils.incr(RedisKeys.fieldServiceOrderNoKey(strDate), 24 * 60 * 60);
        }
        return String.format("%s%s%04d", prefix, strDate, newEquipment);
    }

    public static String getFieldServiceInvestigationNumber(String prefix) {
        String regTime = LocalTimeUtil.getRealCurrentTime();
        String strDate = regTime.substring(0, 10).replace("-", "");
        RedisClientUtils redisClientUtils = (RedisClientUtils) ApplicationContextUtils.getBean("redisClientUtils");
        Long newEquipment = redisClientUtils.incr(RedisKeys.fieldServiceInvestigationNoKey(strDate), 24 * 60 * 60);
        while (newEquipment.equals(0L)) {
            newEquipment = redisClientUtils.incr(RedisKeys.fieldServiceInvestigationNoKey(strDate), 24 * 60 * 60);
        }
        return String.format("%s%s%04d", prefix, strDate, newEquipment);
    }

    public static String getEscalationOrderNumber(String prefix) {
        String regTime = LocalTimeUtil.getRealCurrentTime();
        String strDate = regTime.substring(0, 10).replace("-", "");
        RedisClientUtils redisClientUtils = (RedisClientUtils) ApplicationContextUtils.getBean("redisClientUtils");
        Long newEquipment = redisClientUtils.incr(RedisKeys.escalationNoKey(strDate), 24 * 60 * 60);
        while (newEquipment.equals(0L)) {
            newEquipment = redisClientUtils.incr(RedisKeys.escalationNoKey(strDate), 24 * 60 * 60);
        }
        return String.format("%s%s%04d", prefix, strDate, newEquipment);
    }


    /**
     * 生成GM权限申请单编号 
     */
    public static String getGMApplyRecordNumber(String prefix) {
        String regTime = LocalTimeUtil.getRealCurrentTime();
        String strDate = regTime.substring(0, 10).replace("-", "");
        RedisClientUtils redisClientUtils = (RedisClientUtils) ApplicationContextUtils.getBean("redisClientUtils");
        Long newEquipment = redisClientUtils.incr(RedisKeys.applyRecordNoKey(strDate), 24 * 60 * 60);
        while (newEquipment.equals(0L)) {
            newEquipment = redisClientUtils.incr(RedisKeys.applyRecordNoKey(strDate), 24 * 60 * 60);
        }
        return String.format("%s%s%04d", prefix, strDate, newEquipment);
    }
    
}
